package demo;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import demo.model.Account;
import demo.model.AccountResponseModel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
@RestController
@RequestMapping("/account")
public class AccountService {
	
    public static void main(String[] args) {
    	SpringApplication.run(AccountService.class,args);
    }

	@Value("${demo.serviceId}")
	private String serviceId;
	
	@Value("${demo.sleepInMilliseconds}")
	private long sleepInMilliseconds;
	
	@Value("${demo.alwaysException}")
	private boolean alwaysException;
	
	@SneakyThrows
    @RequestMapping(value="/list/{name}")
    public ResponseEntity<AccountResponseModel> getAccountsByCustomerName(@PathVariable String name,@RequestParam String region) {
    	
		log.debug("****getAccountsByCustomerName(\"{}\",\"{}\")",name,region);
		
    	//simulate hard exception
    	if("UK".equals(region)){
    		if(alwaysException) {
    			throw new Exception("Unsupported region :" + region);
    		}
    	}
    	else if("US".equals(region)){
    		//simulate timeout exception
    		try{
    			Thread.sleep(sleepInMilliseconds);
    		}catch(InterruptedException e){
    		}
    	}    
    	
    	List<Account> accounts = new ArrayList<Account>();
    	
    	accounts.add(new Account("1",10000D,name,region));
    	
    	return ResponseEntity.ok(new AccountResponseModel(serviceId,accounts));
    } 
	
	@SneakyThrows
    @RequestMapping(value="/batchlist/{names}")
    public ResponseEntity<List<AccountResponseModel>> getAccountsByBatch(@PathVariable String names,@RequestParam String regions) {
    	
		log.debug("****getAccountsByBatch(\"{}\",\"{}\")",names,regions);
		
		String[] name = names.split(",");
		String[] region = regions.split(",");
		if(name.length != region.length) {
			throw new Exception("Unmatched paramters");
		}
		
    	List<AccountResponseModel> response = new ArrayList<AccountResponseModel>();
    	
    	for(int i = 0; i < name.length; i++) {
    		List<Account> accounts = new ArrayList<Account>();        	
        	accounts.add(new Account((i+1)+"",10000D * (i + 1),name[i],region[i]));        	
    		response.add(new AccountResponseModel(serviceId,accounts));    		
    	}
    	
    	return ResponseEntity.ok(response);
    } 
	
	@RequestMapping(value = "/greeting", method = RequestMethod.GET)
	public String greeting() {
		return "Greeting from " + serviceId;
    }
}